package stream

import (
	"github.com/gogo/protobuf/proto"
	"github.com/golang/snappy"
	"github.com/prometheus/prometheus/prompb"
	"gluenet/pkg/apis"
)

func StreamAllocator(c *apis.StorageContext) {
	for {
		select {
		case <-c.Context.Done():
			return
		case data := <-c.MetricsChan:
			reqBuf, err := snappy.Decode(nil, data)
			if err != nil {
				c.Logger.Errorf("snappy decode failed %v", err)
				return
			}
			var req prompb.WriteRequest
			if err := proto.Unmarshal(reqBuf, &req); err != nil {
				c.Logger.Errorf("proto unmarshal failed %v", err)
				return
			}

			//c.Logger.Debugf("req %v", req)

			for _, ts := range req.Timeseries {
				switch ts.Labels[0].Value {
				case "info":
					info(c, &ts)
				case "metrics":
					metrics(c, &ts)
				case "status":
					status(c, &ts)
				}
			}
		}
	}
}
